<template>
  <div class="app-container">
    <el-tabs type="border-card">
      <el-tab-pane label="需求反馈">
        <el-row v-if="!convertBuiness">
          <el-col :span="24">
            <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
              <el-form-item label="需求主题" prop="projectName">
                <el-input
                  v-model="queryParams.projectName"
                  placeholder="请输入需求主题"
                  clearable
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>
            <div style="width: 100%;height: 90vh;overflow:auto; margin-bottom: 30%;padding-bottom: 18%;position: relative;">
            <el-table :row-style="{ height: '30px' }" :cell-style="{ padding: '0px' }" height="450" border v-loading="loading" :data="releaseList" >
              <el-table-column label="序号" type="index" :index="indexMethod" />
              <el-table-column label="状态" align="center" prop="status" />
              <el-table-column label="时间" align="center" prop="creationTime" width="120" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.creationTime, '{y}-{m}-{d} ') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="发布单位" align="center" prop="sendUnit" :show-overflow-tooltip="true"/>
              <el-table-column label="发布人" align="center" prop="contacts" :show-overflow-tooltip="true"/>
              <el-table-column label="联系方式" align="center" prop="phone" :show-overflow-tooltip="true"/>
              <el-table-column label="需求主题" align="center" prop="projectName" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                  <template>
                    <!-- 使用按钮作为可点击元素，并绑定点击事件 -->
                    <el-button @click="handleSelect(scope.row,scope.$index)" type="text">
                      {{ scope.row.projectName }}
                    </el-button>
                  </template>
                </template>
              </el-table-column>
              <el-table-column label="发送方式" align="center" prop="sendType" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                  <span>{{ scope.row.sendType === 'oneClick' ? '一键发送' : '分类群发' }}</span>
                </template>
              </el-table-column>
              <el-table-column label="附件" align="center">
                <template slot-scope="scope">
                  <el-button v-if="scope.row.url != null && scope.row.url != ''" @click="handlePictureCardPreview(scope.row.uploadFile)" size="small"
                             type="text">附件</el-button>
                  <el-button size="small" v-else type="text">无附件</el-button>
                </template>
              </el-table-column>
              <el-table-column label="是否转商机" align="center" prop="isBusiness" :show-overflow-tooltip="true"/>
              <el-table-column
                label="商机阶段"
                align="center"
                prop="businessState"
                width="90"
                :show-overflow-tooltip="true"
              >
                <template slot-scope="scope">
                  <dict-tag
                    :options="dict.type.business_state"
                    :value="scope.row.businessState"
                  />
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="85px">
                <template slot-scope="scope">
                  <el-button
                    :disabled="scope.row.bidding || scope.row.status=='已关闭'" size="mini" type="text" icon="el-icon-s-flag" @click="handleBidding(scope.row)" title="标为招标">
                  </el-button>
                  <el-button v-if="$store.state.user.info.companyType==='1'" size="mini" type="text" icon="el-icon-s-opportunity" @click="convertBusiness(scope.row)"  title="转为商机"></el-button>
                </template>
              </el-table-column>
            </el-table>

            <pagination
              v-show="total>0"
              :total="total"
              :page.sync="queryParams.pageNum"
              :limit.sync="queryParams.pageSize"
              @pagination="getList"
            />
            </div>
          </el-col>
        </el-row>

        <!-- 添加或修改需求发布对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body @close="cancel">
          <el-form ref="form" :model="form" :rules="rules" label-width="80px" :inline="true">
            <div v-if="addSign !== 0">
              <el-descriptions title="">
                <el-descriptions-item label="序号">
                  <el-tag size="medium">{{ form.index }}</el-tag>
                </el-descriptions-item>
              </el-descriptions>
            </div>
            <el-form-item label="需求主题" prop="projectName">
              <el-input v-model="form.projectName" placeholder="请输入需求主题" :disabled="ReadOnly"/>
            </el-form-item>
            <el-form-item label="提报人" prop="submitter">
              <el-input v-model="form.submitter" placeholder="请输入提报人" :disabled="ReadOnly"/>
            </el-form-item>
            <el-form-item label="发布人" prop="contacts">
              <el-input v-model="form.contacts" placeholder="请输入发布人" disabled="true"/>
            </el-form-item>
            <el-form-item label="发布人联系方式" prop="phone" label-width="100px">
              <el-input v-model="form.phone" placeholder="请输入电话" :disabled="ReadOnly" style="width: 160px"/>
            </el-form-item>
            <el-form-item label="需求方" prop="demander">
              <el-input v-model="form.demander" placeholder="请输入需求方" :disabled="ReadOnly"/>
            </el-form-item>
            <el-form-item label="需求方联系方式" prop="demander" label-width="100px">
              <el-input v-model="form.demandPhone" placeholder="请输入需求人联系方式" :disabled="ReadOnly" style="width: 160px"/>
            </el-form-item>
            <el-form-item label="要求时间" prop="requiredTime" >
              <el-date-picker clearable v-model="form.requiredTime" type="date" value-format="yyyy-MM-dd"
                              placeholder="请选择时间" style="width: 178px" :disabled="ReadOnly">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="时间" prop="creationTime" >
              <el-date-picker clearable v-model="form.creationTime" type="date" value-format="yyyy-MM-dd"
                              placeholder="请选择时间" style="width: 178px" :disabled="true">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="发布单位" prop="enterpriseName">
              <el-input v-model="form.sendUnit" placeholder="请输入发布单位"  disabled/>
            </el-form-item>
            <el-form-item label="需求状态" prop="status">
              <el-input v-model="form.status" placeholder="请输入需求状态"  disabled/>
            </el-form-item>
            <el-form-item label="详情介绍" prop="projectOverview">
              <el-input v-model="form.projectOverview" type="textarea" placeholder="详情介绍" style="width: 440px"  maxlength="300" show-word-limit :disabled="ReadOnly"/>
            </el-form-item>
            <el-form-item label="附件" prop="uploadFile" label-width="110px">
              <file-view v-if="show" :limit="1" :type="typeSelect" :businessId="form.uploadFile" :businessCode="typeCode"
                         style="width: 500px" ></file-view>
            </el-form-item>
          </el-form>
        </el-dialog>
        <SelectRoleUser ref="select" :type="userType" :sign="sign" :menuId="menuId" @ok="selectUser">
        </SelectRoleUser>
        <!--    附件展示对话框-->
        <el-dialog title="查看附件" :visible.sync="openFileList" width="500px" append-to-body v-if="openFileList">
          <UploadFile :upload-id="uploadId" :business-code="typeCode"></UploadFile>
        </el-dialog>
        <div v-if="convertBuiness">
          <Operate @change="closeEle" :opt="edit" :uuid="uuid" :isClick="isClick" :business-name="bussinessInfo.projectName" :business-id="bussinessInfo.projectId"
                   :businessProvider="bussinessInfo.contacts" :enterprise-name="bussinessInfo.sendUnit" :phone="bussinessInfo.phone" :demander="bussinessInfo.demander" :demandPhone="bussinessInfo.demandPhone" v-if="convertBuiness"></Operate>
        </div>
      </el-tab-pane>
      <el-tab-pane label="需求发布" v-if="this.$store.state.user.info.companyType != '0'">
        <DemandRelease></DemandRelease>
      </el-tab-pane>
    </el-tabs>
  </div>

</template>

<script>
import { listRelease, getRelease, receiveList, listExamine, joinBidding } from "@/api/demand/release";
import { listType } from "@/api/maintype/type";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import MainType from "../../../components/MainType/index.vue";
import BusinessInfo from "../../enterprise/info/index.vue";
import FileView from "@/components/FileView/index";
import UploadFile from "../../upload/index.vue";
import SelectUser from "../../system/role/selectUser.vue";
import SelectRoleUser from "../../system/role/selectUser.vue";
import sendAll from "../../enterprise/info/sendAll.vue";
import { listArea } from "@/api/area/area";
import Operate from "../../enterprise/businessinfo/operate.vue";
import DemandRelease from "../release/index.vue";
export default {
  name: "Release",
  dicts: ["business_state"],
  components: {
    Operate, sendAll, SelectRoleUser, SelectUser, UploadFile, BusinessInfo, MainType,FileView,Treeselect,DemandRelease},
  data() {
    return {
      // 遮罩层
      loading: true,
      openUser:false,
      uploadId:null,
      convertBuiness: false,
      // 选中数组
      ids: [],
      //主分类
      options: null,
      bussinessInfo: {},
      isClick:null,
      userType: "1",
      sign: '2',
      menuId:'2118',
      openFileList:false,
      // 显示搜索条件
      showSearch: true,
      typeCode: 'demand',
      // 总条数
      total: 0,
      edit: 'add',
      uuid: "",
      // 需求发布表格数据
      releaseList: [],
      show:false,
      //行政区域
      optionsArea:null,
      // 弹出层标题
      title: "",
      ReadOnly:false,
      examineRead:false,
      typeSelect:0,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        projectName: null,
      },
      addSign:0,
      // 表单参数
      form: {},
    };
  },
  created() {
    this.area();
    this.getList();
    this.listType();
    if (this.$route.query.id!==undefined){
      this.form.projectId=this.$route.query.id;
      this.handleSelect(this.form,0);
    }
  },
  methods: {
    /** 查询需求发布列表 */
    getList() {
      this.loading = true;
      receiveList(this.queryParams).then(response => {
        this.releaseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //转商机
    convertBusiness(row){
      this.uuid = this.guid();
      this.edit = "add";
      this.isClick = true;
      this.bussinessInfo=row;
      console.log(this.bussinessInfo)
      this.convertBuiness = true;
    },
    guid() {
      function S4() {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
      }
      return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
    },
    //竞标
    handleBidding(row){
      this.$modal.confirm('是否确认参与竞标？').then(function () {
        return joinBidding(row.projectId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("参与竞标成功");
      }).catch(() => { });
    },
    handlePictureCardPreview(id) {
      this.uploadId=id;
      this.openFileList = true;
    },
    closeEle() {
      this.convertBuiness = false;
    },
    // 取消按钮
    cancel() {
      this.show=false;
      this.open = false;
      this.reset();
    },
    close() {
      this.openUser = false
    },
    //获取当前用户在企业管理中的企业信息
    selectEnterprise(selection) {
      this.form.enterpriseId = selection.map(item => item.enterpriseId);
      this.form.receiverName = selection.map(item => item.enterpriseName).join(',');
      console.log(this.form)
      this.close()
    },
    //序号展示
    indexMethod(index) {
      return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1;
    },
    selectOpen(type) {
      this.userType = 1;
      this.$refs.select.show();
    },
    //树点击事件
    getNode(data, node, obj) {
      console.log(data)
      if (data.parentId != '00000000-0000-0000-0000-000000000000') {
        this.queryParams.classificationId = data.id
      } else {
        this.queryParams.classificationId = null
      }
      this.getList()
    },
    selectUser(userId, userName) {
      this.$set(this.form, 'examineId', userId)
      this.$set(this.form, 'examineName', userName)
    },
    //获取树节点数据
    listType() {
      listType({ typeCode: 'demand' }).then(res => {
        console.log(res);
        this.options = res.data;
      })
    },
    //点击接收方文本域,弹出选择当前用户在企业管理中的企业信息
    handleTextAreaFocus() {
      this.openUser = true;
    },
    // 表单重置
    reset() {
      this.form = {
        projectId: null,
        projectName: null,
        projectCondition: null,
        projectOverview: null,
        projectStandard: null,
        projectLocation: null,
        contacts: null,
        phone: null,
        address: null,
        createId: null,
        creationName: null,
        uploadFile: null,
        receiverId: null,
        receiverName: null,
        operateTime: null,
        status: null,
        operateId: null,
        operateName: null,
        departmentId: null,
        departmentName: null,
        organizationId: null,
        organizationName: null,
        belongTo: null,
        classificationId: null,
        examineId: null,
        examineName: null,
        idPath: null,
        sendType: null,
        sendUnit: null,
        endReason: null,
        examineStatus: null,
        projectPicture: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    area(){
      listArea({}).then(response => {
        this.optionsArea = response.data;
      });
    },
    normalizer(node){
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id:node.areaId,
        label:node.areaName,
        isDisabled:node.parentId=='000000'?true:false,
        children:node.list.length>0?node.list:0,
      }
    },
    /** 查看按钮 */
    handleSelect(row,index){
      this.reset();
      this.listType();
      this.area();
      const projectId = row.projectId || this.ids
      getRelease(projectId).then(response => {
        this.form = response.data;
        this.typeSelect = 2;
        this.addSign = 2;
        this.form.index = index + 1;
        this.form.enterpriseName=this.$store.state.user.info.orgName;
        this.ReadOnly=true;
        this.examineRead=true;
        this.show = true;
        this.open = true;
        this.title = "查看需求发布";
      });
    },
  }
};
</script>
